package org.xiyu.yee.beacon_anti_phantom.particle;

import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.BlockParticleOption;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.Blocks;
import org.xiyu.yee.beacon_anti_phantom.beacon.BeaconManager;

import java.util.Map;

public class ParticleManager {
    private static ParticleManager instance;
    private int tickCounter = 0;
    
    private ParticleManager() {}
    
    public static ParticleManager getInstance() {
        if (instance == null) {
            instance = new ParticleManager();
        }
        return instance;
    }
    
    /**
     * 在服务器tick时更新粒子效果
     */
    public void tick(ServerLevel level) {
        tickCounter++;
        
        // 每10tick（0.5秒）更新一次粒子
        if (tickCounter % 10 == 0) {
            spawnBeaconParticles(level);
        }
    }
    
    /**
     * 为所有激活的信标生成粒子圆圈
     */
    private void spawnBeaconParticles(ServerLevel level) {
        Map<BlockPos, Integer> activeBeacons = BeaconManager.getInstance().getActiveBeacons();
        
        for (Map.Entry<BlockPos, Integer> entry : activeBeacons.entrySet()) {
            BlockPos beaconPos = entry.getKey();
            int beaconLevel = entry.getValue();
            int radius = BeaconManager.getInstance().getBeaconRadius(beaconPos);
            
            if (radius > 0) {
                spawnCircleParticles(level, beaconPos, radius, beaconLevel);
            }
        }
    }
    
    /**
     * 在指定位置生成圆形粒子
     */
    private void spawnCircleParticles(ServerLevel level, BlockPos center, int radius, int beaconLevel) {
        BlockParticleOption particleType = getParticleType(beaconLevel);
        
        // 计算圆周上的粒子点
        int numParticles = Math.max(16, radius * 2); // 根据半径调整粒子数量
        
        for (int i = 0; i < numParticles; i++) {
            double angle = 2 * Math.PI * i / numParticles;
            double x = center.getX() + 0.5 + radius * Math.cos(angle);
            double z = center.getZ() + 0.5 + radius * Math.sin(angle);
            double y = center.getY() + 0.5;
            
            // 在圆周上生成粒子
            level.sendParticles(particleType, x, y, z, 1, 0.0, 0.0, 0.0, 0.0);
            
            // 添加一些随机的高度变化使效果更自然
            if (Math.random() < 0.3) {
                level.sendParticles(particleType, x, y + 0.2, z, 1, 0.0, 0.1, 0.0, 0.0);
            }
        }
    }
    
    /**
     * 根据信标等级获取对应的粒子类型
     */
    private BlockParticleOption getParticleType(int beaconLevel) {
        return switch (beaconLevel) {
            case 1 -> new BlockParticleOption(ParticleTypes.BLOCK, Blocks.REDSTONE_BLOCK.defaultBlockState());
            case 2 -> new BlockParticleOption(ParticleTypes.BLOCK, Blocks.GOLD_BLOCK.defaultBlockState());
            case 3 -> new BlockParticleOption(ParticleTypes.BLOCK, Blocks.DIAMOND_BLOCK.defaultBlockState());
            case 4 -> new BlockParticleOption(ParticleTypes.BLOCK, Blocks.NETHERITE_BLOCK.defaultBlockState());
            default -> new BlockParticleOption(ParticleTypes.BLOCK, Blocks.REDSTONE_BLOCK.defaultBlockState());
        };
    }
}
